package com.slf.engine.services.impl;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.jsp.tagext.Tag;

import org.apache.log4j.Logger;
import org.apache.log4j.Priority;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;

import com.slf.engine.bo.Order;
import com.slf.engine.common.SysContants;
import com.slf.engine.exception.EngineException;
import com.slf.engine.http.HttpClientManager;
import com.slf.engine.http.HttpResponseWrapper;
import com.slf.engine.services.IZrcmService;
import com.slf.engine.utils.DateUtils;
import com.slf.engine.utils.KeyedDigestMD5;

public class ZrcmServiceImpl implements IZrcmService {

	private static final Logger log = Logger.getLogger(ZrcmServiceImpl.class);
	
	public Order doPay(Order order)
	{
		StringBuffer url = new StringBuffer();
		url.append("userid=" + SysContants.ZRCM_LOGINNAME);
		url.append("&productid=");
		url.append("&price=" + order.getMoney());
		url.append("&num=1");
		url.append("&mobile=" + order.getAccount());
		url.append("&spordertime=" + DateUtils.strDate("yyyyMMddHHmmss"));
		url.append("&sporderid=" + order.getOrderId());
		url.append("&sign=" + KeyedDigestMD5.getKeyedDigest(url.toString() + "&key=" + SysContants.ZRCM_KEY, ""));
		url.append("&productType=1");
		url.append("&back_url=" + SysContants.ZRCM_NOTICEURL);
		
		log.log(Priority.INFO, "充值提交：" + url.toString());
		HttpClientManager manager = new HttpClientManager();
		try {
			HttpResponseWrapper wrapper = manager.execute("GET", SysContants.ZRCM_URL + "?" + url.toString(), SysContants.DEFAULT_CHARSET, null, null, null, null, null);
			String body = new String(wrapper.getBody(), SysContants.DEFAULT_CHARSET);
			log.log(Priority.INFO, "充值返回：" + body);
			HtmlCleaner cleaner = new HtmlCleaner();
			TagNode node = cleaner.clean(body);
			Object obj[] = node.evaluateXPath("//order/resultno/text()");
			if(obj != null && obj.length > 0)
			{
				StringBuffer s = (StringBuffer)obj[0];
				if("0".equals(s.toString()) || "5006".equals(s.toString()) || "2".equals(s.toString()))
				{
					order.setClbz(SysContants.CZ_RUN);
					obj = node.evaluateXPath("//order/orderid/text()");
					if(obj != null && obj.length > 0)
					{
						s = (StringBuffer)obj[0];
						order.setYyslsh(s.toString());
					}
				}else if("9999".equals(s.toString())){
					order.setClbz(SysContants.CZ_UNKNOW);
					order.setCljg("未知错误，人工查询，status：" + s);
				}else{
					order.setClbz(SysContants.CZ_FAILURE);
					order.setCljg("充值提交失败，status：" + s);
				}
			}else{
				return doPay(order);
			}
		} catch (EngineException e) {
			// TODO Auto-generated catch block
			return doPay(order);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
		} catch (IOException e) {
			// TODO Auto-generated catch block
		} catch (XPatherException e) {
			// TODO Auto-generated catch block
		}
		return order;
	}
	
	public Order doQuery(Order order)
	{
		StringBuffer url = new StringBuffer(SysContants.ZRCM_QURL);
		url.append("?userid=" + SysContants.ZRCM_LOGINNAME);
		url.append("&sporderid=" + order.getOrderId());
		HttpClientManager manager = new HttpClientManager();
		try {
			log.log(Priority.INFO, "查询提交：" + url.toString());
			HttpResponseWrapper wrapper = manager.execute("GET", url.toString(), SysContants.DEFAULT_CHARSET, null, null, null, null, null);
			String body = new String(wrapper.getBody(), SysContants.DEFAULT_CHARSET);
			log.log(Priority.INFO, "查询返回：" + body);
			
			HtmlCleaner cleaner = new HtmlCleaner();
			TagNode node = cleaner.clean(body);
			
			Object obj[] = node.evaluateXPath("//order/resultno/text()");
			if(obj != null && obj.length > 0)
			{
				StringBuffer s = (StringBuffer)obj[0];
				if("1".equals(s.toString()))
				{
					order.setClbz(SysContants.CZ_SUCCESS);
					order.setCljg("充值成功");
				}else if("0".equals(s.toString()) || "2".equals(s.toString())){
					order.setClbz(SysContants.CZ_RUN);
				}else if("9999".equals(s.toString())){
					order.setClbz(SysContants.CZ_UNKNOW);
					order.setCljg("未知错误，人工查询,status:" + s.toString());
				}else{
					order.setClbz(SysContants.CZ_FAILURE);
					order.setCljg("充值失败,status:" + s.toString());
				}
			}else{
				return doQuery(order);
			}
		} catch (EngineException e) {
			// TODO Auto-generated catch block
			
			return doQuery(order);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
		} catch (IOException e) {
			// TODO Auto-generated catch block
		} catch (XPatherException e) {
			// TODO Auto-generated catch block
		}
		return order;
	}
	
	public static void main(String args[])
	{
		Order order = new Order();
		order.setAccount("18627777692");
		order.setMoney("50");
		order.setOrderId("12312311");
		
		new ZrcmServiceImpl().doPay(order);
	}
}
